package edu.hrbu.kwaidu.util;

import cn.hutool.json.JSONUtil;
import edu.hrbu.kwaidu.constant.SystemConstant;
import edu.hrbu.kwaidu.exception.CustomException;
import edu.hrbu.kwaidu.model.entity.SysUser;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static edu.hrbu.kwaidu.constant.SystemConstant.REDIS_HALF_TTL;
import static edu.hrbu.kwaidu.constant.SystemConstant.REDIS_PERMS_PREFIX;

/**
 * @author xudengyu
 */
@Component
public class RedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 组装tokenKey
     *
     * @param token token
     * @return tokenKey
     */
    private String getTokenKey(String token) {
        String phone = CustomUtil.getDecodeToken(token);
        return SystemConstant.REDIS_TOKEN_PREFIX + phone;
    }

    /**
     * 组装permsKey
     *
     * @param token token
     * @return permsKey
     */
    private String getPermsKey(String token) {
        String phone = CustomUtil.getDecodeToken(token);
        return REDIS_PERMS_PREFIX + phone;
    }


    /**
     * 设置当前用户缓存
     *
     * @param token   token
     * @param sysUser 用户信息
     */
    public void setCurrentUserCache(String token, SysUser sysUser) {
        String tokenKey = getTokenKey(token);
        String val = JSONUtil.toJsonStr(sysUser);
        stringRedisTemplate.opsForValue().set(tokenKey, val, REDIS_HALF_TTL, TimeUnit.MINUTES);
    }

    /**
     * 获取当前用户缓存
     *
     * @param token 手机号
     * @return 用户信息
     */
    public SysUser getCurrentUserCache(String token) {
        String tokenKey = getTokenKey(token);
        String userCacheStr = stringRedisTemplate.opsForValue().get(tokenKey);
        return JSONUtil.toBean(userCacheStr, SysUser.class);
    }

    /**
     * 删除当前用户缓存
     *
     * @param token token
     */
    public void removeCurrentUserCache(String token) {
        String tokenKey = getTokenKey(token);
//        System.out.println(">>> deleteTokenKey:" + tokenKey);
        Boolean f = stringRedisTemplate.delete(tokenKey);
        if (Boolean.FALSE.equals(f)) throw new CustomException(SystemConstant.EXCEPTION_REMOVE_FAIL);
    }

    /**
     * 设置权限缓存
     *
     * @param token token
     * @param perms 权限
     */
    public void setPermsCache(String token, List<String> perms) {
        String permsKey = getPermsKey(token);
        String permsCacheStr = JSONUtil.toJsonStr(perms);
        stringRedisTemplate.opsForValue().set(permsKey, permsCacheStr, REDIS_HALF_TTL, TimeUnit.MINUTES);
    }

    /**
     * 获取权限缓存
     *
     * @param token token
     * @return permsCache>List
     */
    public List<String> getPermsCache(String token) {
        String permsKey = getPermsKey(token);
        String permsStr = stringRedisTemplate.opsForValue().get(permsKey);
        return JSONUtil.toList(permsStr, String.class);
    }

    public void removePermsCache(String token) {
        String permsKey = getPermsKey(token);
        stringRedisTemplate.delete(permsKey);
    }


}
